package com.cs.controller.carousel;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cs.controller.utils.R;
import com.cs.pojo.CarouselMap;
import com.cs.pojo.Goods;
import com.cs.service.carousel.ICarouselService;
import com.cs.service.image.ImageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/image")
public class CarouselController {
    @Autowired
    private ICarouselService iCarouselService;
    @Autowired
    private ImageService imageService;

    /**
     * 添加轮播图
     * @param files
     * @param carouselMap
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @Transactional
    public R save(@RequestParam("file")MultipartFile[] files,@ModelAttribute CarouselMap carouselMap) throws Exception {
        ArrayList<String> list = imageService.uploadFileAvatar(files);
        carouselMap.setImages(list.toString());
        if(iCarouselService.save(carouselMap)) {
            return new R("200", "上传成功");
        }else {
            return new R("400","上传失败");
        }
    }

    /**
     * 根据id修改轮播图
     * @param files
     * @param carouselMap
     * @return
     * @throws Exception
     */
    @PutMapping
    @Transactional
    public R update(@RequestParam(value = "file",required = false) MultipartFile[] files,@ModelAttribute CarouselMap carouselMap) throws Exception {
        ArrayList<String> resultImg=new ArrayList<>();
        String imgStr= carouselMap.getImages();
        String[] newImg;
        if(imgStr!=null&&!imgStr.equals("")){
            newImg=imgStr.substring(1, carouselMap.getImages().length()).split(",");
            for (String s : newImg) {
                if(s!=""){
                    int i = s.lastIndexOf("/");
                    String s1=s.substring(i+1);
                    resultImg.add(s1);
                }
            }
        }
        if(files!=null){
            ArrayList<String> list = imageService.uploadFileAvatar(files);
            for (String s : list) {
                resultImg.add(s);
            }
        }
        carouselMap.setImages(resultImg.toString());
//        return null;
        return new R("200",iCarouselService.updateById(carouselMap),"修改成功");
    }

    /**
     * 根据id删除轮播图记录
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R deleteCarousel(@PathVariable Integer id){
        return new R("200",iCarouselService.removeById(id));
    }


    /**
     * 分页获取轮播图
     * @param currentPage
     * @param size
     * @return
     * @throws Exception
     */
    @GetMapping("/page/{currentPage}/{size}")
    public R getByPage(@PathVariable int currentPage,@PathVariable int size) throws Exception {
        IPage<CarouselMap> page = iCarouselService.getPage(currentPage, size);
        if(currentPage>page.getPages()){
            page=iCarouselService.getPage((int)page.getPages(),size);
        }
        return new R("200",page);
    }

    /**
     * 根据主题获取数据
     * @param topic
     * @return
     */
    @GetMapping("/{topic}")
    public R getByTopic(@PathVariable String topic){
        List<CarouselMap> byTopic = iCarouselService.getByTopic(topic);
        return new R("200",byTopic);
    }

    /**
     * 根据主题获取分页数据
     * @param currentPage
     * @param size
     * @param topic
     * @return
     * @throws Exception
     */
    @GetMapping("/page/{currentPage}/{size}/{topic}")
    public R getByPageByTopic(@PathVariable int currentPage,@PathVariable int size,@PathVariable String topic) throws Exception {
        IPage<CarouselMap> page = iCarouselService.getPageByTopic(currentPage, size,topic);
        if(currentPage>page.getPages()){
            page=iCarouselService.getPage((int)page.getPages(),size);
        }
        return new R("200",page);
    }


    /**
     * 获取在用轮播图（小程序使用）
     * @return
     */
    @GetMapping()
    public R getInUsing(){
        return new R("200",iCarouselService.getInUsing());
    }

    /**
     * 设置轮播图使用状态
     * @param id
     * @return
     */
    @PutMapping("/update/{id}")
    public R setUsing(@PathVariable int id){
        return new R("200",iCarouselService.setUsingCarousel(id),"修改成功");
    }

}
